package com.m3839.sdk.login.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.m3839.sdk.common.base.BaseDialogFragment;
import com.m3839.sdk.common.core.Constant;
import com.m3839.sdk.common.core.ErrorConstant;
import com.m3839.sdk.common.util.AppUtils;
import com.m3839.sdk.common.util.FastClickUtils;
import com.m3839.sdk.common.util.ResourcesUtils;
import com.m3839.sdk.login.core.LoginManager;
import com.m3839.sdk.login.bean.InternalTip;

/**
 * Description:用来账号封禁，快爆下载的对话框展示
 *
 * @author 陈宝阳
 * @create 2020/8/7 15:38
 */
public class LoginTipDialogFragment extends BaseDialogFragment {

  private TextView tipContentTv;
  private Button negativeBtn;
  private Button positiveBtn;

  private InternalTip tip;

  public static LoginTipDialogFragment newInstance(InternalTip tip) {
    LoginTipDialogFragment fragment = new LoginTipDialogFragment();
    Bundle data = new Bundle();
    data.putParcelable(Constant.HYKB_KEY_TIP, tip);
    fragment.setArguments(data);
    return fragment;
  }


  public void update(InternalTip tip) {
    this.tip = tip;
    setContent();
    initListener();
  }

  @Override
  protected void parseArgs() {
    tip = getArguments().getParcelable(Constant.HYKB_KEY_TIP);
  }

  @Override
  protected String getLayoutName() {
    return "hykb_login_tip_dialog_fragment";
  }

  @Override
  protected void initView(View view) {
    tipContentTv = view.findViewById(getId("tv_tip_content"));
    negativeBtn = view.findViewById(getId("dlg_btn_negative"));
    positiveBtn = view.findViewById(getId("dlg_btn_positive"));
  }

  @Override
  protected void setContent() {
    if (tip != null) {
      tipContentTv.setText(tip.tipContent);
      negativeBtn.setText(tip.negativeBtnName);
      if (!TextUtils.isEmpty(tip.positiveBtnName)) {
        positiveBtn.setText(tip.positiveBtnName);
        positiveBtn.setVisibility(View.VISIBLE);
        negativeBtn.setBackgroundResource(
            ResourcesUtils.getDrawableId(getActivity(), "hykb_bg_default_dialog_left_btn"));
      } else {
        positiveBtn.setVisibility(View.GONE);
        negativeBtn.setBackgroundResource(
            ResourcesUtils.getDrawableId(getActivity(), "hykb_bg_default_dialog_single_btn"));
      }
    }
  }

  @Override
  protected void initListener() {
    negativeBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        handleAction(tip.negativeBtnAction);
      }
    });

    positiveBtn.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (!TextUtils.isEmpty(tip.positiveBtnName)) {
          handleAction(tip.positiveBtnAction);
        }
      }
    });
  }

  private void handleAction(int action) {
    switch (action) {
      case Constant.HYKB_ACTION_TIP_POSITIVE_LOGIN_AGAIN_CLOSE:
        LoginManager.getInstance()
                .handleErrorCallback(ErrorConstant.HYKB_CODE_LOGIN_AGAIN_CLOSE,
                        ErrorConstant.ERR_MSG_LOGIN_AGAIN_CLOSE);
        dismiss();
        break;
      case Constant.HYKB_ACTION_TIP_NEGATIVE_EXIT_GAME:
        if (FastClickUtils.isFastDoubleClick()) {
          return;
        }
        dismiss();
        AppUtils.killAllProcess(getActivity());
        break;
      case Constant.HYKB_ACTION_TIP_POSITIVE_SWITCH_ACCOUNT:
      case Constant.HYKB_ACTION_TIP_POSITIVE_LOGIN_AGAIN:
        // 调用快爆的切换账号
        LoginManager.getInstance().switchUSerInternal();
        break;
      default:
        break;

    }
  }

}
